<?php

namespace App\Http\Controllers\Admin\Customer;

use App\Exports\CustomerRecBillOrderExport;
use App\Exports\CustomerRecBillTakeOrderExport;
use App\Http\Controllers\Controller;
use App\Http\Requests\customer\priceRequest;
use App\Models\cainiao\AsnModel;
use App\Models\customer\PriceModel;
use App\Models\finance\ReceivableGenericModel;
use App\Models\lazada\BigBagModel;
use App\Models\OrderModel;
use App\Models\RecBillModel;
use App\Models\TakeOrderModel;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class RecBill extends Controller
{

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $customer_id = $request->input('customer_id', '');
        $status = $request->input('status', '');
        $create_start = $request->input('startTime', '');
        $create_end = $request->input('endTime', '');

        $query = RecBillModel::with(['customer'])->WhereHas('customer', function($query){
            $query->where('is_big', 1);
        });

        if($customer_id){
            $query = $query->where('customer_id', $customer_id);
        }

        if($status){
            $query = $query->where('bill_status', $status);
        }

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'customer_id' => $item->customer_id,
                'customer_name' => $item->customer->name ?? '',
                'sn' => $item->sn,
                'take_order_count' => $item->take_order_count,
                'big_count' => $item->big_count,
                'small_count' => $item->small_count,
                'start' => $item->start,
                'end' => $item->end,
                'bill_status' => $item->bill_status,
                'status_text' => RecBillModel::BILL_STATUS_MAPS[$item->bill_status] ?? '',
                'currency' => $item->currency,
                'amount' => $item->amount,
                'created_at' => $item->created_at->format('Y-m-d H:i:s')
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    /**
     * Notes:
     * User: xiangzi
     * Date: 2024/11/7
     * Time: 下午2:04
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function order_list(Request $request) {
        $id = $request->input('id');
        $find = RecBillModel::find($id);
        if(!$find){
            return $this->error('数据不存在');
        }

        $big_list = BigBagModel::with(['take_order'])->where('rec_bill_id', $id)->get();

        $list = $big_list->map(function($item) {
            $flight = $item->take_order->flight ?? '';
            $start_port_code = $arrive_port_code = '';
            $take_off_time = $landing_time = '';
            $origin = $destination =  '';
            if($flight){
                $start_port_code = $flight->start_port_code ?? '';
                $arrive_port_code = $flight->arrive_port_code ?? '';
                $take_off_time = $flight->take_off_time ?? '';
                $landing_time = $flight->landing_time ?? '';
                $origin = $flight->start_port_country_name ?? '';
                $destination = $flight->arrive_port_country_name ?? '';
            }

            $price = ReceivableGenericModel::where('take_order_id', $item->take_order_id)->value('big_line_price') ?? 0;

            $res = [
                'id' => $item->id,
                'get_date' => $item->created_at->format('Y-m-d'),
                'take_order_sn' => $item->take_order->sn ?? '',
                'bigBagID' => $item->bigBagID,
                'transportType' => 'Air',
                'goods_attr' => OrderModel::GOODS_ATTR_MAPS[$item->cargoType] ?? '',
                'origin' => $origin,
                'destination' => $destination,
                'start_port_code' => $start_port_code,
                'arrive_port_code' => $arrive_port_code,
                'take_off_time' => $take_off_time,
                'landing_time' => $landing_time,
                'number' => $item->parcelQty,
                'weight' => round($item->bigBagWeight / 1000, 2),
                'price' => $price,
                'total_amount' => round($item->bigBagWeight / 1000 * $price, 2),
            ];
            return $res;

        });


        return $this->success(['list' => $list]);
    }


    public function order_export(Request $request) {
        $id = $request->input('id');
        $find = RecBillModel::find($id);
        if(!$find){
            return $this->error('数据不存在');
        }

        $price = $find->big_line_price;

        $big_list = BigBagModel::with(['take_order'])->where('rec_bill_id', $id)->get();

        $list = $big_list->map(function($item) use ($price) {
            $flight = $item->take_order->flight ?? '';
            $start_port_code = $arrive_port_code = '';
            $take_off_time = $landing_time = '';
            if($flight){
                $start_port_code = $flight->start_port_code ?? '';
                $arrive_port_code = $flight->arrive_port_code ?? '';
                $take_off_time = $flight->take_off_time ?? '';
                $landing_time = $flight->landing_time ?? '';
            }
            $res = [
                'get_date' => $item->created_at->format('Y-m-d'),
                'take_order_sn' => $item->take_order->sn ?? '',
                'bigBagID' => $item->bigBagID,
                'transportType' => 'Air',
                'goods_attr' => OrderModel::GOODS_ATTR_MAPS[$item->cargoType] ?? '',
//                'origin' => $item->origin,
//                'destination' => $item->destination,
                'start_port_code' => $start_port_code,
                'arrive_port_code' => $arrive_port_code,
                'take_off_time' => $take_off_time,
                'landing_time' => $landing_time,
                'number' => $item->parcelQty,
                'weight' => round($item->bigBagWeight / 1000, 2),
                'price' => $price,
                'total_amount' => round($item->bigBagWeight / 1000 * $price, 2),
            ];
            return $res;

        });


        return Excel::download(new CustomerRecBillOrderExport($list), '大包明细.xlsx');
    }


    public function take_order_list(Request $request) {
        $id = $request->input('id');
        $find = RecBillModel::find($id);
        if(!$find){
            return $this->error('数据不存在');
        }

        $take_order = ReceivableGenericModel::with(['take_order', 'flight', 'notify'])->where('bill_id', $id)->get();

        $list = $take_order->map(function($item) {
            $res = [
                'take_order_sn' => $item->take_order->sn ?? '',
                'landing_time' => $item->flight->landing_time ?? '',
                'start_port_code' => $item->flight->start_port_code ?? '',
                'arrive_port_code' => $item->flight->arrive_port_code ?? '',
                'transportType' => 'Air',
                'goods_attr' => OrderModel::GOODS_ATTR_MAPS[$item->goods_attr] ?? '',
                'take_off_time' => $item->flight->take_off_time ?? '',
                'big_num' => $item->package_num,
                'small_num' => $item->notify?->sum('parcelQty') ?? 0,
                'gross_weight' => $item->gross_weight,
                'charging_weight' => $item->charging_weight,
                'volume' => round($item->volume_weight / 167, 2),
                'take_warehouse' => $item->take_order->take_warehouse ?? '',
                'price' => $item->big_line_price,
                'total_amount' => $item->total_amount,
            ];
            return $res;
        });

        return $this->success(['list' => $list]);
    }


    public function take_order_export(Request $request) {
        $id = $request->input('id');
        $find = RecBillModel::find($id);
        if(!$find){
            return $this->error('数据不存在');
        }

        $take_order = ReceivableGenericModel::with(['take_order', 'flight', 'notify'])->where('bill_id', $id)->get();

        $list = $take_order->map(function($item) {
            $res = [
                'take_order_sn' => $item->take_order->sn ?? '',
                'landing_time' => $item->flight->landing_time ?? '',
                'start_port_code' => $item->flight->start_port_code ?? '',
                'arrive_port_code' => $item->flight->arrive_port_code ?? '',
                'transportType' => 'Air',
                'goods_attr' => OrderModel::GOODS_ATTR_MAPS[$item->goods_attr] ?? '',
                'take_off_time' => $item->flight->take_off_time ?? '',
                'big_num' => $item->package_num,
                'small_num' => $item->notify?->sum('parcelQty') ?? 0,
                'gross_weight' => $item->gross_weight,
                'charging_weight' => $item->charging_weight,
                'volume' => round($item->volume_weight / 167, 2),
                'take_warehouse' => $item->take_order->warehouse_name ?? '',
                'price' => $item->big_line_price,
                'total_amount' => $item->total_amount,
            ];
            return $res;
        });

        return Excel::download(new CustomerRecBillTakeOrderExport($list), '提单明细.xlsx');
    }





}
